/*
 * (C) Copyright 2009
 * Sean Chiang, <n2i911@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include <ucos_ii.h>
#include <types.h>
#include <common.h>
#include <console.h>

#define TASK_STK_SIZE	1024			/* Size of each task's stacks */
#define NO_TASKS	10			/* Number of tasks */

OS_STK TaskStk[NO_TASKS][TASK_STK_SIZE];	/* Tasks stacks */
OS_STK SuperTaskStk[TASK_STK_SIZE];		/* stack of superstack */
OS_STK SvcStk[TASK_STK_SIZE];			/* stack of svc mode */
OS_STK IrqStk[TASK_STK_SIZE];			/* stack of irq mode */
OS_STK FiqStk[TASK_STK_SIZE];			/* stack of irq mode */

void task0(void *p_arg)
{
	while(1) {
		OSTimeDly(50);
		printf("I'm task0\n");
	}
}

void task1(void *p_arg)
{
	while(1) {
		OSTimeDly(25);
		printf("I'm task1\n");
	}
}

void task2(void *p_arg)
{
	while(1) {
		OSTimeDly(100);
		printf("I'm task2\n");
	}
}

void supertask(void *p_arg)
{
	OSTaskCreate(task0, NULL, (void *)&TaskStk[0][TASK_STK_SIZE - 1], 1);
	OSTaskCreate(task1, NULL, (void *)&TaskStk[1][TASK_STK_SIZE - 1], 2);
	OSTaskCreate(task2, NULL, (void *)&TaskStk[2][TASK_STK_SIZE - 1], 3);

	init_clocks();

	while(1) {
		OSTimeDly(200);
		printf("I'm super task\n");
	}
}

void start_kernel(void)
{
	OSInit();
	/* The memory stack growth is from high to low */
	OSTaskCreate(supertask, NULL, (void *)&SuperTaskStk[TASK_STK_SIZE - 1], 0);
	OSStart();
}
